package com.leyou.common.advice;

import com.leyou.common.exception.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author ShenMin
 * @date 2021/7/12 14:39
 * @description
 */
//相当于给对应的controller加了try
@ControllerAdvice
@Slf4j
public class ControllerExceptionAdvice {
    @ExceptionHandler(org.springframework.jdbc.BadSqlGrammarException.class)
    public ResponseEntity<String> handleRuntimeException(org.springframework.jdbc.BadSqlGrammarException ex) {
        log.error("出事了{}", ex);
        return ResponseEntity.status(400).body(ex.getMessage());
    }

    //相当于catch
    @ExceptionHandler(LyException.class)
    public ResponseEntity<String> handleException(LyException ex) {
        log.error("出事了{}", ex);
        return ResponseEntity.status(ex.getStatus()).body(ex.getMessage());
    }


}
